package org.travel_system.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.travel_system.pojo.Performance;
import org.travel_system.service.PerformanceService;
import org.travel_system.utils.Result;

import java.util.List;

@RestController
@RequestMapping("/performances")
public class PerformanceController {

    public final PerformanceService performanceService;

    @Autowired
    public PerformanceController(PerformanceService performanceService) {
        this.performanceService = performanceService;
    }

    //查询演出列表
    @GetMapping
    public Result<List<Performance>> getPerformances() {
        return Result.success(performanceService.getPerformances());
    }

    //查询单个演出项目
    @GetMapping("/{name}")
    public Result<Performance> getPerformance(@PathVariable String name) {
        return Result.success(performanceService.getPerformanceByName(name));
    }

    //添加演出
    @PostMapping
    public Result<?> addPerformance(@RequestBody Performance performance) {
        if (performanceService.getPerformanceByName(performance.getPerformanceName())!= null) {
            return Result.error("演出已存在");
        }
        return performanceService.addPerformance(performance) > 0 ?
                Result.success() :
                Result.error("添加演出失败");
    }

    //更新演出信息
    @PutMapping
    public Result<?> updatePerformance(@RequestBody Performance performance) {
        if (performanceService.getPerformanceByName(performance.getPerformanceName()) == null) {
            return Result.error("演出不存在");
        }
        return performanceService.updatePerformance(performance) > 0 ?
                Result.success() :
                Result.error("更新失败");
    }

    //删除演出信息
    @DeleteMapping("/{name}")
    public Result<?> deletePerformance(@PathVariable String name) {
        if (performanceService.getPerformanceByName(name) == null) {
            return Result.error("演出不存在");
        }
        return performanceService.deletePerformance(name) > 0 ?
                Result.success() :
                Result.error("删除失败");

    }
}
